var proCode = null;
var proName = null;
var proContact = null;
var proPhone = null;
var proDesc = null;
var saveBtn = null;
var backBtn = null;

$(function(){
	proCode = $("#proCode")
	proName = $("#proName")
	proContact = $("#proContact");
	proPhone = $("#proPhone");
	proDesc = $("#proDesc");
	saveBtn = $("#save");
	backBtn = $("#back");
	
	//初始化的时候，要把所有的提示信息变为：* 以提示必填项，更灵活，不要写在页面上
	proCode.next().html("*")
	proName.next().html("*")
	proContact.next().html("*");
	proPhone.next().html("*");

/*	proCode.bind("blur",function () {
		// ajax后台验证--proCode是否已存在
		$.ajax({
			type:"GET",
			url:path+"/provider/checkProCode",
			data:{proCode:proCode.val()},
			dataType:"json",
			success:function (data) {
				if (data.message == "exist") {
					validateTip(proCode.next(),{"color":"red"}, " 该编码已存在",false);
				} else if (data.message == "null"){
					validateTip(proCode.next(),{"color":"red"}," 该编码不可以为空",false);
				} else {
					validateTip(proCode.next(),{"color":"green"},imgYes+" ",true);
				}
			},
			error:function (data){
				validateTip(proCode.next(),{"color":"red"},imgNo+" 您访问的页面不存在",false);
			}
		})
	})*/


	/*
	 * 验证
	 * 失焦\获焦
	 * jquery的方法传递
	 */
/*	proCode.on("focus",function(){
		validateTip(proCode.next(),{"color":"#666666"},"* 请输入供应商编码",false);
	}).on("blur",function(){
		if(proCode.val() != null && proCode.val() != ""){
			validateTip(proCode.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(proCode.next(),{"color":"red"},imgNo+" 供应商编码不能为空，请重新输入",false);
		}

	});*/

	proName.on("focus",function(){
		validateTip(proName.next(),{"color":"#666666"},"* 请输入供应商名称",false);
	}).on("blur",function(){
		if(proName.val() != null && proName.val() != ""){
			validateTip(proName.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(proName.next(),{"color":"red"},imgNo+" 供应商名称不能为空，请重新输入",false);
		}

	});

	proContact.on("focus",function(){
		validateTip(proContact.next(),{"color":"#666666"},"* 请输入联系人",false);
	}).on("blur",function(){
		if(proContact.val() != null && proContact.val() != ""){
			validateTip(proContact.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(proContact.next(),{"color":"red"},imgNo+" 联系人不能为空，请重新输入",false);
		}
		
	});
	
	proPhone.on("focus",function(){
		validateTip(proPhone.next(),{"color":"#666666"},"* 请输入手机号",false);
	}).on("blur",function(){
		var patrn=/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
		if(proPhone.val().match(patrn)){
			validateTip(proPhone.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(proPhone.next(),{"color":"red"},imgNo + " 您输入的手机号格式不正确",false);
		}
	});

/*	proDesc.on("focus",function(){
		validateTip(proDesc.next(),{"color":"#666666"},"* 请简要描述(50字内)",false);
	}).on("blur",function(){
		if(proDesc.val().length<50){
			validateTip(proDesc.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(proDesc.next(),{"color":"red"},imgNo+" 请简要描述(50字内)",false);
		}
	});*/
	
	saveBtn.on("click",function(){
		proName.blur();
		proContact.blur();
		proPhone.blur();
		proDesc.blur();
		if(
			proName.attr("validateStatus") == "true" &&
			proContact.attr("validateStatus") == "true" &&
				proPhone.attr("validateStatus") == "true"/*&&
				proDesc.attr("validateStatus") == "true"*/){
			if(confirm("是否确认提交数据")){
				$("#providerForm").submit();
			}
		}
	});
	
	backBtn.on("click",function(){
		//alert("modify: "+referer);
		if(referer != undefined 
			&& null != referer 
			&& "" != referer
			&& "null" != referer
			&& referer.length > 4){
		 window.location.href = referer;
		}else{
			history.back(-1);
		}
	});
});